package slick.path2glory.tutorial1;

import java.awt.Font;
import java.util.ArrayList;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class GameplayState extends BasicGameState {

	private int stateId = 0;
	ArrayList<CollidableImageObject> platforms = null;
	final int numPlatforms = 4;

	Player aF = null;
	Player dAF = null;

	Player aF2 = null;
	Player dAF2 = null;

	PainBox ouch = null;

	// CollidableImageObject awesomeFace = null;

	Image aFImg = null;
	Image dAFImg = null;
	Image[] aFAnim = null;
	Image[] dAFAnim = null;
	Image platformImg = null;

	float aFStartX = 400;
	float aFStartY = 200;

	float dAFStartX = 400;
	float dAFStartY = 200;

	float aF2StartX = 650;
	float aF2StartY = 200;

	float dAF2StartX = 650;
	float dAF2StartY = 200;

	float deltaX = 0;
	float deltaY = 0;

	float scale = 1;

	long time;
	long elapsedTime;

	Input input;

	// Vector2f awesomeFaceVel;

	public GameplayState(int stateId) {
		this.stateId = stateId;
	}

	@Override
	public int getID() {
		return stateId;
	}

	@Override
	public void init(GameContainer gc, StateBasedGame sb) throws SlickException {
		// awesomeFaceVel = new Vector2f(0, 0);

		time = gc.getTime();
		elapsedTime = gc.getTime();

		afInit();
		dafInit();
		platformsInit();
		ouch = new PainBox(new Vector2f(250, 440));

	}

	@Override
	public void enter(GameContainer gc, StateBasedGame sb)
			throws SlickException {
		super.enter(gc, sb);

	}

	@Override
	public void render(GameContainer gc, StateBasedGame sb, Graphics g)
			throws SlickException {

		drawPlatforms(g);

		if (SlickBasicGame.players >= 1) {
			p1render(g);
		}
		if (SlickBasicGame.players >= 2) {
			p2render(g);
		}
		ouch.render(g);

	}

	@Override
	public void update(GameContainer gc, StateBasedGame sb, int delta)
			throws SlickException {

		elapsedTime = gc.getTime();
		elapsedTime -= time;
		deltaX = 0;
		deltaY = 0;
		String p1commands = "";
		String p2commands = "";
		input = gc.getInput();

		// Checks if you pressed escape and exits to menu if true.
		escapeCheck(sb);

		// Checks what keys are being pressed.
		p1commands += p1movementCheck(p1commands);
		p2commands += p2movementCheck(p2commands);

		// Checks how many players there are and updates the correct
		// player/players.
		if (SlickBasicGame.players >= 1) {
			p1update(p1commands, delta);
		}
		if (SlickBasicGame.players >= 2) {
			p2update(p2commands, delta);
		}

	}
	
	public void platformsInit() throws SlickException {
		platformImg = new Image("data/test_tile_mid.png");

		Vector2f platformPos;
		Rectangle platformHitbox = new Rectangle(0, 0, platformImg.getWidth(),
				platformImg.getHeight());

		platforms = new ArrayList<CollidableImageObject>();
		for (int i = 0; i < numPlatforms; i++) {
			platformPos = new Vector2f(200 + i * 200, 150 + i * 150);
			platforms.add(new CollidableImageObject("Floor" + i, platformImg,
					platformPos, platformHitbox, 1));

		}
		platforms.add(new CollidableImageObject("Floor", platformImg,
				new Vector2f(200, 500), platformHitbox, 1));
	}

	public void afInit() throws SlickException {
		aFImg = new Image("data/AF_N/AF_N1.png");
		aFAnim = new Image[1];
		aFAnim[0] = aFImg;

		Vector2f awesomeFacePos = new Vector2f(aFStartX, aFStartY);
		Vector2f awesomeFacePos2 = new Vector2f(aF2StartX, aF2StartY);
		Circle awesomeFaceHitsphere = new Circle(aFImg.getWidth() / 2,
				aFImg.getHeight() / 2, aFImg.getWidth() / 2);

		aF = new AwesomeFace(awesomeFacePos, awesomeFaceHitsphere, 1);
		aF2 = new AwesomeFace(awesomeFacePos2, awesomeFaceHitsphere, 1);
	}

	public void dafInit() throws SlickException {
		dAFImg = new Image("data/DAF/DAF1.png");
		dAFAnim = new Image[1];
		dAFAnim[0] = dAFImg;

		Vector2f demonAwesomeFacePos = new Vector2f(dAFStartX, dAFStartY);
		Circle demonAwesomeFaceHitsphere = new Circle(dAFImg.getWidth() / 2,
				dAFImg.getHeight() / 2, dAFImg.getWidth() / 2);

		dAF = new DemonAwesomeFace(demonAwesomeFacePos,
				demonAwesomeFaceHitsphere, 1);
		dAF2 = new DemonAwesomeFace(demonAwesomeFacePos,
				demonAwesomeFaceHitsphere, 1);
	}

	private boolean checkCollisions(Player player) { // returns true if a
		// collision has
		// occurred
		for (int i = 0; i < platforms.size(); i++) {
			if (player.isCollidingWith(platforms.get(i))) {
				// System.out.println("Collided #"+i);
				return true;
			}
		}
		return false;
	}

	private void drawPlatforms(Graphics g) {
		for (int i = 0; i < platforms.size(); i++) {
			platforms.get(i).render(g);
		}
	}

	public void p1render(Graphics g) {
		if (SlickBasicGame.player1character == 1) {
			aF.render(g);
		} else if (SlickBasicGame.player1character == 2) {
			dAF.render(g);
		}
	}

	public void p2render(Graphics g) {
		if (SlickBasicGame.player2character == 1) {
			aF2.render(g);
		} else if (SlickBasicGame.player2character == 2) {
			dAF2.render(g);
		}
	}

	public String p1movementCheck(String p1commands) {

		if (input.isKeyDown(Input.KEY_W)) {
			p1commands += "UP1";
		}
		if (input.isKeyDown(Input.KEY_S)) {
			p1commands += "DOWN1";
		}
		if (input.isKeyDown(Input.KEY_A)) {
			p1commands += "LEFT1";
		}
		if (input.isKeyDown(Input.KEY_D)) {
			p1commands += "RIGHT1";
		}

		return p1commands;
	}

	public String p2movementCheck(String p2commands) {
		if (SlickBasicGame.players >= 2) {
			if (input.isKeyDown(Input.KEY_UP)) {
				p2commands += "UP2";
			}
			if (input.isKeyDown(Input.KEY_DOWN)) {
				p2commands += "DOWN2";
			}
			if (input.isKeyDown(Input.KEY_LEFT)) {
				p2commands += "LEFT2";
			}
			if (input.isKeyDown(Input.KEY_RIGHT)) {
				p2commands += "RIGHT2";
			}
		}

		return p2commands;
	}

	public void p1update(String p1commands, int delta) throws SlickException {
		if (SlickBasicGame.player1character == 1) {
			Vector2f changeAFPos = aF.updatePlayer(p1commands, elapsedTime,
					delta);

			aF.setPosition(new Vector2f(aF.getPosition().x + changeAFPos.x, aF
					.getPosition().y + changeAFPos.y));

			if (checkCollisions(aF) || aF.isCollidingWith(ouch)) {
				if (aF.isCollidingWith(ouch)) {
					aF.hit(1);
				} else {
					aF.playerVel = new Vector2f(0, 0);
				}

				aF.setPosition(new Vector2f(aF.getPosition().x - changeAFPos.x,
						aF.getPosition().y - changeAFPos.y));
				aF.jumpNum = 2;
				aF.fallTime = 0;
			} else {

				if (aF.fallTime == 0) {
					aF.fallTime = elapsedTime;
				} else if ((aF.jumpNum == 2)
						&& ((elapsedTime - aF.fallTime) > 110)) {
					aF.jumpNum--;
				}

			}

			if ((aF.getPosition().x < -100)
					|| (aF.getPosition().y < -100)
					|| (aF.getPosition().x > SlickBasicGame.SCREEN_WIDTH + 100)
					|| (aF.getPosition().y > SlickBasicGame.SCREEN_HEIGHT + 100)) {
				aF.setPosition(new Vector2f(aFStartX, aFStartY));
				aF.reset();
			}
		} else if (SlickBasicGame.player1character == 2) {
			Vector2f changeDAFPos = dAF.updatePlayer(p1commands, elapsedTime,
					delta);

			dAF.setPosition(new Vector2f(dAF.getPosition().x + changeDAFPos.x,
					dAF.getPosition().y + changeDAFPos.y));

			if (checkCollisions(dAF) || dAF.isCollidingWith(ouch)) {
				if (dAF.isCollidingWith(ouch)) {
					dAF.hit(1);
				} else {
					dAF.playerVel = new Vector2f(0, 0);
				}

				dAF.setPosition(new Vector2f(dAF.getPosition().x
						- changeDAFPos.x, dAF.getPosition().y - changeDAFPos.y));
				dAF.jumpNum = 2;
				dAF.fallTime = 0;
			} else {

				if (dAF.fallTime == 0) {
					dAF.fallTime = elapsedTime;
				} else if ((dAF.jumpNum == 2)
						&& ((elapsedTime - dAF.fallTime) > 110)) {
					dAF.jumpNum--;
				}

			}

			if ((dAF.getPosition().x < -100)
					|| (dAF.getPosition().y < -100)
					|| (dAF.getPosition().x > SlickBasicGame.SCREEN_WIDTH + 100)
					|| (dAF.getPosition().y > SlickBasicGame.SCREEN_HEIGHT + 100)) {
				dAF.setPosition(new Vector2f(dAFStartX, dAFStartY));
				dAF.reset();
			}
		}
	}

	public void p2update(String p2commands, int delta) throws SlickException {
		if (SlickBasicGame.player2character == 1) {
			Vector2f changeAF2Pos = aF2.updatePlayer(p2commands, elapsedTime,
					delta);

			aF2.setPosition(new Vector2f(aF2.getPosition().x + changeAF2Pos.x,
					aF2.getPosition().y + changeAF2Pos.y));

			if (checkCollisions(aF2) || aF2.isCollidingWith(ouch)) {
				if (aF2.isCollidingWith(ouch)) {
					aF2.hit(1);
				} else {
					aF2.playerVel = new Vector2f(0, 0);
				}

				aF2.setPosition(new Vector2f(aF2.getPosition().x
						- changeAF2Pos.x, aF2.getPosition().y - changeAF2Pos.y));
				aF2.jumpNum = 2;
				aF2.fallTime = 0;
			} else {

				if (aF2.fallTime == 0) {
					aF2.fallTime = elapsedTime;
				} else if ((aF2.jumpNum == 2)
						&& ((elapsedTime - aF2.fallTime) > 110)) {
					aF2.jumpNum--;
				}

			}

			if ((aF2.getPosition().x < -100)
					|| (aF2.getPosition().y < -100)
					|| (aF2.getPosition().x > SlickBasicGame.SCREEN_WIDTH + 100)
					|| (aF2.getPosition().y > SlickBasicGame.SCREEN_HEIGHT + 100)) {
				aF2.setPosition(new Vector2f(aF2StartX, aF2StartY));
				aF2.reset();
			}
		} else if (SlickBasicGame.player2character == 2) {
			Vector2f changeDAFPos = dAF2.updatePlayer(p2commands, elapsedTime,
					delta);

			dAF2.setPosition(new Vector2f(
					dAF2.getPosition().x + changeDAFPos.x, dAF2.getPosition().y
							+ changeDAFPos.y));

			if (checkCollisions(dAF2) || dAF2.isCollidingWith(ouch)) {
				if (dAF2.isCollidingWith(ouch)) {
					dAF2.hit(1);
				} else {
					dAF2.playerVel = new Vector2f(0, 0);
				}

				dAF2.setPosition(new Vector2f(dAF2.getPosition().x
						- changeDAFPos.x, dAF2.getPosition().y - changeDAFPos.y));
				dAF2.jumpNum = 2;
				dAF2.fallTime = 0;
			} else {

				if (dAF2.fallTime == 0) {
					dAF2.fallTime = elapsedTime;
				} else if ((dAF2.jumpNum == 2)
						&& ((elapsedTime - dAF2.fallTime) > 110)) {
					dAF2.jumpNum--;
				}

			}

			if ((dAF2.getPosition().x < -100)
					|| (dAF2.getPosition().y < -100)
					|| (dAF2.getPosition().x > SlickBasicGame.SCREEN_WIDTH + 100)
					|| (dAF2.getPosition().y > SlickBasicGame.SCREEN_HEIGHT + 100)) {
				dAF2.setPosition(new Vector2f(dAF2StartX, dAF2StartY));
				dAF2.reset();
			}
		}
	}

	public void escapeCheck(StateBasedGame sb) {
		if (input.isKeyDown(Input.KEY_ESCAPE)) {
			aF.setPosition(new Vector2f(aFStartX, aFStartY));
			dAF.setPosition(new Vector2f(aFStartX, aFStartY));
			dAF.reset();
			aF.reset();
			if (SlickBasicGame.players >= 2) {
				aF2.setPosition(new Vector2f(aF2StartX, aF2StartY));
				dAF2.setPosition(new Vector2f(aF2StartX, aF2StartY));
				dAF2.reset();
				aF2.reset();
			}
			sb.enterState(SlickBasicGame.MAINMENUSTATE);
		}
	}

}
